<template>
	<view class="home" :class="{ 'fade-in': isLoaded }">
		<view class="head" :class="{ 'slide-down': isLoaded }">
			<view class="title">
				<text class="text">{{ title }}</text>
				<cover-image class="indexbjpoint" src="/static/indexbjpoint.png"></cover-image>
			</view>
			<view v-if="status == 1" class="test-label"></view>
			<view class="banner">
				<cover-image class="jiudian" :src="banner"></cover-image>
			</view>
			<view class="position">
				<cover-image class="positioning" src="/static/positioning.png"></cover-image>
				<text class="text">{{ addressName }}</text>
			</view>
		</view>
		<view class="content" :class="{ 'slide-up': isLoaded }">
			<view class="item" v-for="(item, index) in classifyList" :key="item" :style="{ animationDelay: `${index * 0.2}s` }" :class="{ 'fade-in': isLoaded }">
				<view class="item-head" :class="`item-head-${index+1}`">
					<cover-image class="zhongcao" :src="classifyImgListMap[item]"></cover-image>
					<text class="text">{{ classifyListMap[item] }}</text>
				</view>
				<view class="app-list">
					<template v-for="app in channelList" :key="app.channelId">
						<view v-if="app.classify == item" class="app" @click="goToReview(app)" :class="{ 'scale-in': isLoaded }">
							<cover-image class="img" :src="channelImgListMap[app.code]"></cover-image>
							<text class="text">{{ app.name }}</text>
						</view>
					</template>
				</view>
			</view>
		</view>
		<text class="bottom">🎁 参加活动可以领福利，详情询问服务员</text>
		<view v-show="showMark" class="mark">
			<view class="p">在默认浏览器打开</view>
			<cover-image class="tips" src="/static/tishi.jpg"></cover-image>
			<cover-image @click="closeMark" class="img" src="/static/guanbi.png"></cover-image>
		</view>

		<uni-popup ref="tool_wechat" type="bottom" background-color="#fff" border-radius="10px 10px 0 0">
			<view class="tool_wechat">
				<view class="tool_wechat_head">
					扫码
				</view>
				<cover-image class="tool_wechat_img" :src="tool_wechat_img" @longpress="saveToolWechatImage"></cover-image>
				<view class="sm">
					长按保存 微信扫码添加店铺微信
				</view>
			</view>
		</uni-popup>

		<uni-popup ref="tool_wifi" type="bottom" background-color="#fff" border-radius="10px 10px 0 0">
			<view class="tool_wifi">
				<view class="tool_wifi_head">
					复制密码连WIFI
				</view>
				<view class="tool_wifi_content">
					<view class="title">
						WIFI名称
					</view>
					<view class="input">
						{{ WIFIName }}
					</view>
					<view class="title">
						密码
					</view>
					<view class="input">
						{{ WIFIPassword }}
						<view class="button_copy" @click="copyWiftPassword">复制</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { classifyListMap, channelImgListMap, classifyImgListMap } from './appList.js'

	export default {
		data() {
			return {
				classifyListMap,
				channelImgListMap,
				classifyImgListMap,
				title: '种草 评论 领福利',
				showMark: false,
				classifyList: [],
				channelList: [],
				addressName: '',
				code: this.getUrlHashParams()?.code,
				banner: '',
				WIFIName: '',
				WIFIPassword: '',
				tool_wechat_img: "",
				status: 0,
				isLoaded: false,
			}
		},
		onLoad() {
			this.getIndexInfo()
			uni.setStorageSync('urlCode', this.code)
			// 添加页面加载动画
			setTimeout(() => {
				this.isLoaded = true
			}, 100)
		},
		methods: {
			saveToolWechatImage() {},
			getIndexInfo() {
				uni.request({
					url: '/app-api/market/store/get?code=' + this.code,
					method: 'GET',
					success: (res) => {
						const data = res.data.data
						this.addressName = data?.name
						this.classifyList = data?.classifyList ?? []
						this.channelList = data?.channelList ?? []
						this.banner = data.img
						this.status = data.status
					}
				});
			},
			copyWiftPassword() {
				uni.setClipboardData({
					data: this.WIFIPassword, // 直接复制带 `\n` 的文本
					success: () => {
						uni.showToast({ title: '复制成功', icon: 'success' });
					},
					fail: () => {
						uni.showToast({ title: '复制失败', icon: 'error' });
					}
				});
			},
			closeMark() {
				this.showMark = false
			},
			goToReview(app) {
				if (app.code === 'tool_wifi') {
					this.$refs.tool_wifi?.open?.('bottom')
					this.WIFIName = app.propertiesObj?.ssid
					this.WIFIPassword = app.propertiesObj?.pw
					return
				}
				if (app.code === 'tool_wechat') {
					this.$refs.tool_wechat?.open?.('bottom')
					this.tool_wechat_img = app.propertiesObj?.image
					return
				}
				if (!this.isInDefaultBrowser()) {
					this.showMark = true
					return
				}
				// 打开对应的评论页面
				const params = {
					storeCode: this.code,
					addressName: this.addressName,
					...app
				};
				const queryString = Object.keys(params)
					.map(key => `${key}=${encodeURIComponent(params[key])}`)
					.join('&');
				const url = `/pages/review/index?${queryString}`;
				uni.navigateTo({ url })
			},
			isInDefaultBrowser() {
				const ua = navigator.userAgent;
				// 判断是否在微信、QQ、微博、小红书等 WebView 里
				const isWeChat = /MicroMessenger/i.test(ua);
				const isQQ = /QQ\//i.test(ua);
				const isWeibo = /Weibo/i.test(ua);
				const isXiaohongshu = /XiaoHongShu/i.test(ua);
				const isAlipay = /Alipay/i.test(ua);
				const isDingTalk = /DingTalk/i.test(ua);

				return !(isWeChat || isQQ || isWeibo || isXiaohongshu || isAlipay || isDingTalk);
			},
			getUrlHashParams(url = window.location.href) {
				const hashUrl = new URL(url)?.hash ?? ''
				// 通过 ? 分割获取后面的参数字符串
				const urlStr = hashUrl.split('?')[1]
				// 创建空对象存储参数
				const obj = {}
				// 再通过 & 将每一个参数单独分割出来
				const paramsArr = urlStr?.split('&')
				if (!paramsArr) return obj
				for (let i = 0, len = paramsArr.length; i < len; i++) {
					// 再通过 = 将每一个参数分割为 key:value 的形式
					const arr = paramsArr[i].split('=')
					obj[decodeURIComponent(arr[0])] = decodeURIComponent(arr[1])
				}
				return obj
			}
		}
	}
</script>

<style scoped lang="scss">
	.home {
		background: url('@/static/indexbg.png');
		position: absolute;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		opacity: 0;
		transition: opacity 0.5s ease-in-out;

		&.fade-in {
			opacity: 1;
		}

		.tool_wechat {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			&_head {
				height: 80rpx;
				line-height: 80rpx;
				font-size: 30rpx;
				color: #000;
			}

			&_img {
				width: 300rpx;
				margin: 30rpx 0 20rpx 0;
				border: 10rpx solid #d2d8ff;
			}

			.sm {
				margin-bottom: 30rpx;
			}
		}

		.tool_wifi {
			color: #000;

			&_head {
				height: 80rpx;
				line-height: 80rpx;
				font-size: 30rpx;
				text-align: center;
			}

			&_content {
				margin: 30rpx 30rpx 70rpx 30rpx;
				text-align: left;

				.title {
					font-size: 28rpx;
					margin-bottom: 20rpx;
					font-weight: bold;
					text-indent: 20rpx;
				}

				.input {
					background: #f6f6f9;
					border-radius: 20rpx;
					height: 85rpx;
					line-height: 85rpx;
					font-size: 28rpx;
					text-indent: 20rpx;
					margin-bottom: 30rpx;
					position: relative;

					.button_copy {
						position: absolute;
						right: 8rpx;
						top: 8rpx;
						background: red;
						color: #1F64FF;
						background-color: #E3EBFF;
						border-radius: 20rpx;
						height: 69rpx;
						line-height: 69rpx;
						width: 100rpx;
					}
				}
			}
		}

		.head {
			padding: 40rpx;
			box-sizing: border-box;
			position: relative;
			transform: translateY(-20px);
			opacity: 0;
			transition: all 0.5s ease-out;

			&.slide-down {
				transform: translateY(0);
				opacity: 1;
			}

			.test-label {
				position: absolute;
				top: 0;
				right: 0;
				background: #ff3e3e;
				padding: 120rpx 180rpx 20rpx 0;
				transform: rotate(45deg) translate(40%, -60%);
				transform-origin: right top;
				z-index: 10;
				box-shadow: 0 4rpx 16rpx rgba(245, 108, 108, 0.18);
			}
			.test-label::after {
				content: "测";
				display: block;
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(10%, 70%);
				color: #fff;
				font-size: 30rpx;
				font-weight: bold;
				white-space: nowrap;
				pointer-events: none;
			}

			.title {
				display: flex;

				.text {
					font-size: 42rpx;
					color: #000000;
					font-weight: bold;
					z-index: 1;
				}

				.indexbjpoint {
					width: 52rpx;
					height: 33rpx;
					position: relative;
					left: -16rpx;
					top: 5rpx;
				}
			}

			.banner {
				border-radius: 30rpx;
				overflow: hidden;
				margin: 40rpx 0 40rpx 0;

				.jiudian {
					width: 100%;
					height: 300rpx;
				}
			}

			.position {
				display: flex;

				.positioning {
					width: 40rpx;
					margin-right: 10rpx;
				}

				.text {
					font-size: 28rpx;
					font-weight: bold;
				}
			}
		}

		.content {
			width: 100%;
			background: #FFFFFF;
			box-shadow: 0rpx -6rpx 20rpx 0rpx rgba(0, 0, 0, 0.12);
			border-radius: 36rpx;
			padding: 44rpx 20rpx;
			box-sizing: border-box;
			transform: translateY(20px);
			opacity: 0;
			transition: all 0.5s ease-out;

			&.slide-up {
				transform: translateY(0);
				opacity: 1;
			}

			.item {
				&-head {
					height: 75rpx;
					background: linear-gradient(270deg, #FFFFFF 0%, #ffede699 100%);
					border-radius: 32rpx;
					display: flex;
					align-items: center;
					padding: 0 26rpx;

					&-2 {
						background: linear-gradient(270deg, #FFFFFF 0%, #e2ebff99 100%);
					}

					&-3 {
						background: linear-gradient(270deg, #FFFFFF 0%, #fff7dc99 100%);
					}

					.zhongcao {
						width: 50rpx;
						height: 50rpx;
						margin-right: 12rpx;
					}

					.text {
						font-size: 34rpx;
						color: #333333;
						font-weight: bold;
					}
				}

				.app-list {
					display: flex;
					margin-bottom: 60rpx;
					flex-wrap: wrap;

					.app {
						width: 25%;
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: center;
						margin-top: 36rpx;
						transform: scale(0.9);
						opacity: 0;
						transition: all 0.3s ease-out;

						&.scale-in {
							transform: scale(1);
							opacity: 1;
						}

						.img {
							width: 96rpx;
							height: 96rpx;
							margin-bottom: 16rpx;
						}

						.text {
							font-size: 24rpx;
						}
					}
				}
			}
		}

		.bottom {
			width: 100%;
			color: #999999;
			font-size: 24rpx;
			text-align: center;
			padding-top: 20rpx;
			box-sizing: border-box;
			display: block;
			background-color: #F5F7F9;
			position: relative;
			top: -20rpx;
			min-height: 80rpx;
		}

		.mark {
			position: fixed;
			top: 0;
			left: 0;
			bottom: 0;
			right: 0;
			background-color: #000000cf;
			z-index: 2;

			.p {
				color: #fff;
				font-size: 36rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				position: absolute;
				width: 100%;
				height: 200rpx;
			}

			.img {
				width: 40rpx;
				height: 40rpx;
				position: absolute;
				left: 50%;
				top: 450rpx;
				margin-left: -20rpx;
				cursor: pointer;
			}
		}

		.test-store-modal {
			position: fixed;
			top: 0;
			left: 0;
			bottom: 0;
			right: 0;
			background-color: rgba(0, 0, 0, 0.6);
			z-index: 999;
			display: flex;
			align-items: center;
			justify-content: center;

			.modal-content {
				width: 560rpx;
				background: #FFFFFF;
				border-radius: 24rpx;
				padding: 40rpx;
				box-sizing: border-box;

				.modal-title {
					font-size: 36rpx;
					font-weight: bold;
					color: #333333;
					text-align: center;
					margin-bottom: 30rpx;
				}

				.modal-message {
					font-size: 32rpx;
					color: #666666;
					text-align: center;
					margin-bottom: 40rpx;
				}

				.modal-button {
					width: 100%;
					height: 88rpx;
					background: #007aff;
					border-radius: 44rpx;
					color: #FFFFFF;
					font-size: 32rpx;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}

		.tips {
			position: absolute;
			left: 50%;
			top: 10%;
			margin-left: -350rpx;
			width: 700rpx;
			height: 240rpx;
			border-radius: 60rpx;
		}

		.app.scale-in .img {
			animation: icon-pop 0.4s cubic-bezier(.68,-0.55,.27,1.55);
		}

		@keyframes icon-pop {
			0% {
				transform: scale(0.7);
				opacity: 0.5;
				box-shadow: none;
			}
			60% {
				transform: scale(1.15);
				opacity: 1;
				box-shadow: 0 12rpx 32rpx rgba(31, 100, 255, 0.18);
			}
			100% {
				transform: scale(1);
				opacity: 1;
				box-shadow: 0 8rpx 24rpx rgba(31, 100, 255, 0.12), 0 1rpx 4rpx rgba(0,0,0,0.06);
			}
		}
	}
</style>